package com.rfchina.okr.xianren.base;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;

public class ErrorActionInterceptor implements Interceptor {

	private static Logger logger = LoggerFactory.getLogger(ErrorActionInterceptor.class);

	public void intercept(Invocation inv) {

		try {
			
			inv.invoke();
		} catch (LoginRunTimeException e) {
			
			return;
		} catch (ErrorRunTimeException e) {
			logger.error(e.getMessage());
			Controller con = inv.getController();
			con.renderText(JsonKit.toJson(new ResultBean(e.getCode(), e.getMessage())));
			return;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			Controller con = inv.getController();
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			e.printStackTrace();
			con.renderText(JsonKit.toJson(new ResultBean(500, e.getMessage(), sw.toString())));
			return;
		}

	}

}
